package com.brillilab.starter.controller.aio.comments;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.brillilab.common.entity.PageVo;
import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.vo.comments.ChatClassifySearchVo;
import com.brillilab.domain.vo.comments.ChatContentSearchVo;
import com.brillilab.domain.vo.comments.ChatFileSearchVo;
import com.brillilab.domain.vo.comments.ChatMemberListVo;
import com.brillilab.domain.vo.comments.ChatMessageAddVo;
import com.brillilab.domain.vo.comments.ChatMessageVo;
import com.brillilab.domain.vo.comments.ChatSearchClassifyVo;
import com.brillilab.domain.vo.comments.ChatSearchFileVo;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.comments.IChatMessageService;
import com.brillilab.service.core.system.IFileManageService;
import com.brillilab.service.logic.comments.CommentsLogic;
import com.brillilab.service.logic.experiments.ExperimentLogic;
import com.brillilab.service.logic.plans.PlanCommentLogic;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.constant.StarterConstant;

/**
 * 评论信息
 * 
 * @author
 * @date 2019/12/03
 */
@RestController
@RequestMapping(value = "/api/chatMessage")
public class ChatMessageController {

	@Resource
	private IChatMessageService chatMessageService;
	@Resource
	private CommentsLogic commentsLogic;
	@Resource
	private UsersLogic usersLogic;
	@Resource
	private IFileManageService fileManageService;
	@Resource
	private ExperimentLogic experimentLogic;
	@Resource
	private PlanCommentLogic planCommentLogic;

	/**
	 * 获取聊天成员列表
	 * 
	 * @param token
	 * @return
	 */
	@GetMapping("/memberList")
	public ResponseVo memberList(@RequestHeader(name = StarterConstant.TOKEN) String token) {
		UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
		List<ChatMemberListVo> voList = commentsLogic.getChatMemberList(userInfoVo.getLabId(), userInfoVo.getLabMemberId());
		return ResponseVo.success(voList);
	}

	/**
	 * 获取历史聊天消息(每次20条)
	 * 
	 * @param otheMemberId   他人成员ID
	 * @param startMessageId 起始消息ID(获取该消息之前的消息,null从最新一条开始获取)
	 * @param token
	 * @return
	 */
	@GetMapping("/history")
	public ResponseVo historyMessage(@RequestParam Long otherMemberId, Long startMessageId,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
		List<ChatMessageVo> list = commentsLogic.historyMessage(userInfoVo.getLabMemberId(), otherMemberId,
				startMessageId);
		return ResponseVo.success(list);
	}

	/**
	 * 获取最新聊天消息
	 * 
	 * @param otherMemberId   他人成员ID
	 * @param startMessageId 起始消息ID(获取该消息ID之后的消息)
	 * @return
	 */
	@GetMapping("/newest")
	public ResponseVo newestMessage(@RequestParam Long otherMemberId, Long startMessageId,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
		List<ChatMessageVo> list = commentsLogic.newestMessage(userInfoVo.getLabMemberId(), otherMemberId,
				startMessageId);
		return ResponseVo.success(list);
	}

	/**
	 * 刪除聊天消息
	 * 
	 * @param messageId
	 * @return
	 */
	@GetMapping("/delete")
	public ResponseVo delete(@RequestParam Long messageId, @RequestHeader(name = StarterConstant.TOKEN) String token) {
		chatMessageService.removeById(messageId);
		return ResponseVo.success();
	}

	/**
	 * 发送聊天信息
	 * 
	 * @param addVo
	 * @return
	 */
	@PostMapping("/send")
	public ResponseVo sendChatMessage(@RequestBody ChatMessageAddVo addVo, @RequestHeader(name = StarterConstant.TOKEN) String token) {
		UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
		commentsLogic.sourceIsDelete(addVo.getClassify(), addVo.getClassifyId());
		Long messageId = commentsLogic.addChatMessage(userInfoVo.getLabId(), userInfoVo.getLabMemberId(), addVo);
		return ResponseVo.successMap("messageId", messageId);
	}

	
	/**
	 * 查找聊天分类记录
	 * 
	 * @param otheMemberId   他人成员ID
	 * @param classify   分类：1实验，2计划，3课题，4研究内容，5实验流
	 * @return
	 */
	@PostMapping("/searchClassify")
	public ResponseVo searchClassify(@RequestBody ChatClassifySearchVo searchVo,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
		searchVo.setMeMemberId(userInfoVo.getLabMemberId());
		PageVo<ChatSearchClassifyVo> page = commentsLogic.searchClassify(searchVo);
		return ResponseVo.success(page);
	}
	
	/**
	 * 查找聊天内容记录
	 * 
	 * @param otheMemberId   他人成员ID
	 * @param classify   分类：1实验，2计划，3课题，4研究内容，5实验流
	 * @return
	 */
	@PostMapping("/searchContent")
	public ResponseVo searchContent(@RequestBody ChatContentSearchVo searchVo,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
		searchVo.setMeMemberId(userInfoVo.getLabMemberId());
		PageVo<ChatMessageVo> page = commentsLogic.searchContent(searchVo);
		return ResponseVo.success(page);
	}
	
	/**
	 * 查找图片记录
	 * 
	 * @param searchVo
	 * @return
	 */
	@PostMapping("/searchPicture")
	public ResponseVo searchPicture(@RequestBody ChatFileSearchVo searchVo, @RequestHeader(name = StarterConstant.TOKEN) String token) {
		UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
		searchVo.setMeMemberId(userInfoVo.getLabMemberId());
		List<ChatSearchFileVo> pictures= commentsLogic.searchPicture(searchVo);
		return ResponseVo.success(pictures);
	}
	
	/**
	 * 查询未读总数
	 * 
	 * @param searchVo
	 * @return
	 */
	@GetMapping("/noReadTotalNum")
	public ResponseVo noReadTotalNum(@RequestHeader(name = StarterConstant.TOKEN) String token) {
		UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
		int noReadTotalNum = commentsLogic.noReadTotalNum(userInfoVo.getLabMemberId());
		return ResponseVo.successMap("noReadTotalNum", noReadTotalNum);
	}
	
	
	/**
	 * 判断消息内容来源是否删除
	 * 
	 * @param classify
	 * @param classifyId
	 * @return
	 */
	@GetMapping("/sourceIsDelete")
	public ResponseVo sourceIsDelete(@RequestParam Integer classify, @RequestParam Long classifyId) {
		commentsLogic.sourceIsDelete(classify, classifyId);
		return ResponseVo.success();
	}
	
	
}
